package spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import spring.domain.User;
import spring.domain.UserLog;
import spring.service.UserService;
import spring.test.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController extends HttpServlet {

    @RequestMapping("/login")
    public void allLogs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String pw = request.getParameter("password");
        User user = null;
        try{
            user = (new UserService()).queryByNameAndPassword(name, pw);
        }catch (Exception e){
            System.out.println(e);
        }
        if(user != null)
        {
            String realpath = request.getServletContext().getRealPath("/files");
            List<String> filesName = FileUtil.getFilesName(realpath);
            request.setAttribute("filesName", filesName);
//            System.out.println("登录成功");
            request.setAttribute("result", "登录成功，欢迎"+name);
            request.getSession().setAttribute("name",name);
            request.getRequestDispatcher("/main.jsp").forward(request,response);
        }
        else
        {
//            System.out.println("登录失败");
            request.setAttribute("result", "登录失败，账号或密码错误！");
//            request.getSession().setAttribute("error", "账号或者密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
////        System.out.println("get");
//        request.getRequestDispatcher("/login.jsp").forward(request,response);
//    }
//
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
////        System.out.println("post");
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=utf-8");
//        String name = request.getParameter("name");
//        String pw = request.getParameter("password");
//        User user = null;
//        try{
//            user = (new UserService()).queryByNameAndPassword(name, pw);
//        }catch (Exception e){
//            System.out.println(e);
//        }
//        if(user != null)
//        {
//            String realpath = request.getServletContext().getRealPath("/files");
//            List<String> filesName = FileUtil.getFilesName(realpath);
//            request.setAttribute("filesName", filesName);
////            System.out.println("登录成功");
//            request.setAttribute("result", "登录成功，欢迎"+name);
//            request.getSession().setAttribute("name",name);
//            request.getRequestDispatcher("/main.jsp").forward(request,response);
//        }
//        else
//        {
////            System.out.println("登录失败");
//            request.setAttribute("result", "登录失败，账号或密码错误！");
////            request.getSession().setAttribute("error", "账号或者密码错误");
//            request.getRequestDispatcher("/login.jsp").forward(request,response);
//        }
//
//    }
}
